import requests
from config import *

class UserApi():
    def __init__(self):
        self.get_user_page_url = MAIN_URL + 'api/v1/order/by_user?page=1'
        self.post_order_url = MAIN_URL + 'api/v1/order'
        self.get_order_info_url = MAIN_URL + 'api/v1/order/{}'

        self.get_address_url = MAIN_URL + 'api/v1/address'
        pass

    # 用户信息
    def get_user_page_response(self):
        url = self.get_user_page_url
        headers = HEADER
        headers["token"] = TOKEN
        return requests.get(url, headers=headers)
    # 提交订单
    def post_order_response(self, pid: list, nums: list):
        url = self.post_order_url
        data = {"products": []}
        for i, j in zip(pid, nums):
            data["products"].append({
                "product_id": i,
                "count": j
            })

        headers = HEADER
        headers['token'] = TOKEN
        print(headers)
        resp = requests.post(url, headers=headers, data=data)
        return resp

    # 获得订单信息
    def get_order_info(self, id_o):
        url = self.get_order_info_url.format(id_o)

        headers = {
            'token': TOKEN
        }
        return requests.get(url, headers=headers)

    # 获得当前订单数量
    def get_max_order(self):
        resp = self.get_user_page_response()
        len_ = len(resp.json()['data'])
        return len_

    # 获得地址信息
    def get_address_response(self):
        url = self.get_address_url
        headers = {
            'token': TOKEN
        }
        return requests.get(url, headers=headers)



user_api = UserApi()